package com.tdk.bts.view.manager;

import java.awt.BorderLayout;
import java.awt.FileDialog;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.tdk.bts.action.FilmAction;
import com.tdk.bts.action.TheaterAction;
import com.tdk.bts.entity.Film;
import com.tdk.bts.entity.Theater;
import com.tdk.bts.util.ImageUtil;
import com.tdk.bts.util.Page;
import com.tdk.bts.util.Request;

public class FilmManagerView extends BaseTabbedPanel{
	
	private JTable dataTable ;
	
	private Page<Film> data;
	
	private FilmAction filmAction = new FilmAction();
	
	private JFrame indexJF;
	
	public FilmManagerView(JFrame indexJF) {
		super();
		this.indexJF = indexJF;
		init();
	}
	
	private void init()
	{
		removeAll();
		
		setLayout(new BorderLayout());
		
		dataTable = new JTable();
		bindData(1);
		add(new JScrollPane(dataTable));
		
		JPanel btnPanel =  new JPanel();
		
		JLabel preLabel = new JLabel(ImageUtil.getImage("pre.png"));
		btnPanel.add(preLabel);

		JLabel nextLabel = new JLabel(ImageUtil.getImage("next.png"));
		btnPanel.add(nextLabel);

		JLabel addLabel = new JLabel(ImageUtil.getImage("addBatch.png"));
		btnPanel.add(addLabel);
		
		JLabel delLabel = new JLabel(ImageUtil.getImage("delBtn.png"));
		btnPanel.add(delLabel);

		JLabel detailLabel = new JLabel(ImageUtil.getImage("detailLabel.png"));
		btnPanel.add(delLabel);
		
		JLabel filmScreenBtn = new JLabel(ImageUtil.getImage("manageScreenBtn.png"));
		btnPanel.add(filmScreenBtn);
		
		
		add(btnPanel,BorderLayout.SOUTH);
		
		preLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				bindData(data.getPrePage());
			}
		});
		
		nextLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				bindData(data.getNextPage());
			}
		});
		
		addLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				FileDialog fd = new FileDialog(indexJF);
				fd.setVisible(true);
				if(fd.getDirectory() != null)
				{
					String filePath = fd.getDirectory() + fd.getFile();
					if(!filePath.endsWith(".xls"))
					{
						JOptionPane.showMessageDialog(null,"请先选中xls文件", "错误", JOptionPane.ERROR_MESSAGE);
						return;
					}
					Request request = new Request();
					request.setParameter("file", filePath);
					
					try {
						filmAction.batchAddFilms(request);
						JOptionPane.showMessageDialog(null, "添加成功", "消息", JOptionPane.INFORMATION_MESSAGE);
					} catch (Exception e1) {
						e1.printStackTrace();
						JOptionPane.showMessageDialog(null, e1.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
					}
				}
				
			}
		});
		
		delLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				int []rows=dataTable.getSelectedRows();

				if(rows.length == 0)
				{
					JOptionPane.showMessageDialog(null,"请先选中要删除的数据", "错误", JOptionPane.ERROR_MESSAGE);
					return;
				}
				StringBuilder sb = new StringBuilder();
				for(int i=0;i<rows.length;i++){
					sb.append(dataTable.getValueAt(rows[i], 0)+",") ;		
				}
				sb.deleteCharAt(sb.length()-1);
				Request request = new Request();
				request.setParameter("ids", sb.toString());
				try {
					filmAction.deleteFilm(request);

					bindData(data.getPageNo());
				} catch (Exception e1) {
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, e1.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		
		filmScreenBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				int index = dataTable.getSelectedRow();
				if(index == -1)
				{
					JOptionPane.showMessageDialog(null,"请先选中要管理的数据", "错误", JOptionPane.ERROR_MESSAGE);
					return;
				}
				
				String id = dataTable.getValueAt(index, 0)+"";
				String name = dataTable.getValueAt(index, 1)+"";
				
				new
						FilmScreenManagerView(FilmManagerView.this,id,name);
			}
		});
	}

	public void bindData(int pageNo) {
		
		DefaultTableModel dtm = new DefaultTableModel();
		dtm.addColumn("编号");
		dtm.addColumn("名称");
		dtm.addColumn("简介");
		dtm.addColumn("封面");
		dtm.addColumn("上映时间");
		
		Request request = new Request();
		request.setParameter("pageNo", pageNo);
		try {
			filmAction.findAllFilms(request);
			data = (Page<Film>) request.getAttribute("filmList");
			
			for (int i = 0; i < data.size(); i++) {
				Film t = data.get(i);
				dtm.addRow(new Object[]{t.getId(),t.getName(),t.getInfo(),t.getPicUrl(),t.getPublishDate()});
			}
			
			dataTable.setModel(dtm);
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
		}
	}
	
}
